<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="Java Python" />
   
  <meta name="description" content="From Zero to Hero" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Java-基础-JVM&amp;JMM |  朱酱酱的学习博客
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="朱酱酱的学习博客" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-JavaInterview/Java-基础-JVM&amp;JMM" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Java-基础-JVM&amp;JMM
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/02/24/JavaInterview/Java-%E5%9F%BA%E7%A1%80-JVM&JMM/" class="article-date">
  <time datetime="2020-02-24T09:38:38.000Z" itemprop="datePublished">2020-02-24</time>
</a>
      
      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">4k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">15分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="JVM探究"><a href="#JVM探究" class="headerlink" title="JVM探究"></a>JVM探究</h1><ul>
<li>请你谈谈对JVM的理解？ java8虚拟机和之前的变化更新？</li>
<li>什么是OOM,什么是栈溢出StackOverFlowError？怎么分析？</li>
<li>JVM的常用调优参数有哪些？</li>
<li>内存快照如何抓取，怎么分析Dump文件？</li>
<li>谈谈JVM中，你对类加载器的认识？</li>
</ul>
<h2 id="需要掌握的内容"><a href="#需要掌握的内容" class="headerlink" title="需要掌握的内容"></a>需要掌握的内容</h2><h3 id="1-JVM的位置"><a href="#1-JVM的位置" class="headerlink" title="1. JVM的位置"></a>1. JVM的位置</h3><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200218175915.png" alt="Jvm的位置"></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200218180047.png" alt=""></p>
<a id="more"></a>

<h3 id="2-JVM的体系结构"><a href="#2-JVM的体系结构" class="headerlink" title="2. JVM的体系结构"></a>2. JVM的体系结构</h3><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200218180637.png" alt="体系架构"></p>
<h3 id="3-类加载器"><a href="#3-类加载器" class="headerlink" title="3. 类加载器"></a>3. 类加载器</h3><ul>
<li>作用：加载class字节码文件  </li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200218203932.png" alt=""></p>
<h3 id="4-类加载器的类别"><a href="#4-类加载器的类别" class="headerlink" title="4. 类加载器的类别"></a>4. 类加载器的类别</h3><h4 id="4-1-BootstrapClassLoader（启动类加载器）"><a href="#4-1-BootstrapClassLoader（启动类加载器）" class="headerlink" title="4.1 BootstrapClassLoader（启动类加载器）"></a>4.1 BootstrapClassLoader（启动类加载器）</h4><p><code>c++</code>编写，加载<code>java</code>核心库 <code>java.*</code>,构造<code>ExtClassLoader</code>和<code>AppClassLoader</code>。由于引导类加载器涉及到虚拟机本地实现细节，开发者无法直接获取到启动类加载器的引用，所以不允许直接通过引用进行操作</p>
<h4 id="4-2-ExtClassLoader-（标准扩展类加载器）"><a href="#4-2-ExtClassLoader-（标准扩展类加载器）" class="headerlink" title="4.2 ExtClassLoader （标准扩展类加载器）"></a>4.2 ExtClassLoader （标准扩展类加载器）</h4><p><code>java</code>编写，加载扩展库，如<code>classpath</code>中的<code>jre</code> ，<code>javax.*</code>或者<br> <code>java.ext.dir</code> 指定位置中的类，开发者可以直接使用标准扩展类加载器。</p>
<h4 id="4-3-AppClassLoader（系统类加载器）"><a href="#4-3-AppClassLoader（系统类加载器）" class="headerlink" title="4.3 AppClassLoader（系统类加载器）"></a>4.3 AppClassLoader（系统类加载器）</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java&#96;编写，加载程序所在的目录，如&#96;user.dir&#96;所在的位置的&#96;class</span><br></pre></td></tr></table></figure>

<h4 id="4-4-CustomClassLoader（用户自定义类加载器）"><a href="#4-4-CustomClassLoader（用户自定义类加载器）" class="headerlink" title="4.4 CustomClassLoader（用户自定义类加载器）"></a>4.4 CustomClassLoader（用户自定义类加载器）</h4><p><code>java</code>编写,用户自定义的类加载器,可加载指定路径的<code>class</code>文件</p>
<h3 id="5-双亲委派机制"><a href="#5-双亲委派机制" class="headerlink" title="5. 双亲委派机制"></a>5. 双亲委派机制</h3><p><strong>5.1 作用：</strong></p>
<p> 1、防止重复加载同一个<code>.class</code>。通过委托去向上面问一问，加载过了，就不用再加载一遍。保证数据安全。<br> 2、保证核心<code>.class</code>不能被篡改。通过委托方式，不会去篡改核心<code>.class</code>，即使篡改也不会去加载，即使加载也不会是同一个<code>.class</code>对象了。不同的加载器加载同一个<code>.class</code>也不是同一个<code>Class</code>对象。这样保证了<code>Class</code>执行安全。</p>
<p><strong>5.2 定义：</strong></p>
<p>当某个类加载器需要加载某个<code>.class</code>文件时，它首先把这个任务委托给他的上级类加载器，递归这个操作，如果上级的类加载器没有加载，自己才会去加载这个类。</p>
<ol>
<li>类加载器收到类加载的请求 </li>
<li>将这个请求向上委托给父类加载器去完成，一直向上委托</li>
<li>启动类加载器检查是否能加载当前这个类，能加载就结束，使用当前的加载器，否则抛出异常，通过子类加载器进行加载</li>
<li>重复步骤3</li>
</ol>
<p><strong>5.3 源码分析</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> Class&lt;?&gt; loadClass(String name, <span class="keyword">boolean</span> resolve)</span><br><span class="line">            <span class="keyword">throws</span> ClassNotFoundException</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">synchronized</span> (getClassLoadingLock(name)) &#123;</span><br><span class="line">            <span class="comment">// 首先检查这个class是否已经加载过了</span></span><br><span class="line">            Class&lt;?&gt; c = findLoadedClass(name);</span><br><span class="line">            <span class="keyword">if</span> (c == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">long</span> t0 = System.nanoTime();</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="comment">// c==null表示没有加载，如果有父类的加载器则让父类加载器加载</span></span><br><span class="line">                    <span class="keyword">if</span> (parent != <span class="keyword">null</span>) &#123;</span><br><span class="line">                        c = parent.loadClass(name, <span class="keyword">false</span>);</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="comment">//如果父类的加载器为空 则说明递归到bootStrapClassloader了</span></span><br><span class="line">                        <span class="comment">//bootStrapClassloader比较特殊无法通过get获取</span></span><br><span class="line">                        c = findBootstrapClassOrNull(name);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;&#125;</span><br><span class="line">                <span class="keyword">if</span> (c == <span class="keyword">null</span>) &#123;</span><br><span class="line">                    <span class="comment">//如果bootstrapClassLoader 仍然没有加载过，则递归回来，尝试自己去加载class</span></span><br><span class="line">                    <span class="keyword">long</span> t1 = System.nanoTime();</span><br><span class="line">                    c = findClass(name);</span><br><span class="line">                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);</span><br><span class="line">                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);</span><br><span class="line">                    sun.misc.PerfCounter.getFindClasses().increment();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (resolve) &#123;</span><br><span class="line">                resolveClass(c);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> c;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<h3 id="6-沙箱安全机制"><a href="#6-沙箱安全机制" class="headerlink" title="6. 沙箱安全机制"></a>6. 沙箱安全机制</h3><ol>
<li><p>什么是沙箱安全机制？</p>
<ul>
<li><p>Java安全模型的核心就是Java沙箱（sandbox）</p>
</li>
<li><p>什么是沙箱？沙箱是一个限制程序运行的环境。沙箱机制就是将 Java 代码限定在虚拟机(JVM)特定的运行范围中，并且严格限制代码对本地系统资源访问，通过这样的措施来保证对代码的有效隔离，防止对本地系统造成破坏。</p>
</li>
<li><p>沙箱<strong>主要限制系统资源访问</strong>，那系统资源包括什么？——<code>CPU、内存、文件系统、网络</code>。不同级别的沙箱对这些资源访问的限制也可以不一样。</p>
</li>
</ul>
</li>
</ol>
<h3 id="7-Native"><a href="#7-Native" class="headerlink" title="7. Native"></a>7. Native</h3><ol>
<li><p>凡是带了native关键字的，表示java的作用范围达不到了，会去调用底层C语言的库！</p>
</li>
<li><p>会进入本地方法栈，会调用本地方法接口（JNI）</p>
</li>
<li><p>JNI的作用：扩展Java的使用，融合不同的编程语言为Java所用 （最初：C，C++）</p>
</li>
<li><p>Java在内存区域中专门开辟了一块标记区域，Native Method Stack, 登记native方法</p>
</li>
<li><p>在最终执行的时候，加载本地方法库中的方法通过JNI</p>
</li>
</ol>
<h3 id="8-PC寄存器"><a href="#8-PC寄存器" class="headerlink" title="8. PC寄存器"></a>8. PC寄存器</h3><p>程序计数器：Program Counter Register</p>
<p>每个线程都有一个程序计数器，是线程私有的，就是一个指针，指向方法区中的字节码文件（用来存储指向像一条指令的地址，也即将要执行的指令代码），在执行引擎读取下一条指令，是一个非常小的内存空间，可以忽略不计</p>
<h3 id="9-方法区"><a href="#9-方法区" class="headerlink" title="9. 方法区"></a>9. 方法区</h3><p>Method Area 方法区</p>
<p>​    方法区是被所有现场共享的，所有字段和方法字节码，以及一些特殊方法，如构造函数，接口代码也在此定义，简单说，所有定义的方法的信息都保存在该区域，此区域属于共享区间</p>
<p><strong>静态变量，常量，类信息（构造方法，接口定义）,运行时的常量池存在方法区中，但是实例变量（variable）存在堆内存中，和方法区无关</strong></p>
<p>用来存放</p>
<ul>
<li><p>static</p>
</li>
<li><p>final</p>
</li>
<li><p>class</p>
</li>
<li><p>常量池</p>
</li>
</ul>
<h3 id="10-栈"><a href="#10-栈" class="headerlink" title="10. 栈"></a>10. 栈</h3><p>程序猿：数据结构 + 算法</p>
<p>码农：     框架 + 业务逻辑</p>
<h4 id="10-1-栈"><a href="#10-1-栈" class="headerlink" title="10.1 栈"></a>10.1 栈</h4><ul>
<li><p>栈：先进后出，后进先出（main方法先执行，最后结束~）</p>
</li>
<li><p>栈，栈内存主管程序的执行，生命周期和线程同步</p>
</li>
<li><p>线程结束，栈内存也就被释放了</p>
</li>
<li><p>对于栈来说，不存在垃圾回收问题（一旦线程结束，栈就Over）</p>
</li>
<li><p>栈：8大基本类型 + 对象的引用（地址） + 实例的方法（method）</p>
</li>
<li><p>原理：栈帧</p>
<p> <img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221103845.png" alt=""></p>
</li>
</ul>
<p> <img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221104938.png" alt=""></p>
<h4 id="10-2-队列"><a href="#10-2-队列" class="headerlink" title="10.2 队列"></a>10.2 队列</h4><p><strong>队列：先进先出（FIFO:First Input First Output）</strong></p>
<p><strong>喝多了吐就是栈，吃多了拉就是队列</strong></p>
<h4 id="10-3-栈溢出"><a href="#10-3-栈溢出" class="headerlink" title="10.3 栈溢出"></a>10.3 栈溢出</h4><p>​    <img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221104002.png" alt=""></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//StackOverflowError</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Zhan</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Zhan().test();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>&#123;</span><br><span class="line">        a();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a</span><span class="params">()</span></span>&#123;</span><br><span class="line">        test();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//栈溢出 ： 递归会有问题 StackOverflow</span></span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br><span class="line">	at Zhan.test(Zhan.java:<span class="number">8</span>)</span><br><span class="line">	at Zhan.a(Zhan.java:<span class="number">12</span>)</span><br></pre></td></tr></table></figure>



<h4 id="10-4-栈-堆-方法区：交互关系"><a href="#10-4-栈-堆-方法区：交互关系" class="headerlink" title="10.4 栈 + 堆 + 方法区：交互关系"></a>10.4 栈 + 堆 + 方法区：交互关系</h4><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200310171936.png" alt=""></p>
<p><strong>画出一个对象实例化的过程（在内存中）： 百度 看视频</strong></p>
<h3 id="11-三种JVM"><a href="#11-三种JVM" class="headerlink" title="11. 三种JVM"></a>11. 三种JVM</h3><ul>
<li>Sun公司 <code>HotSpot</code></li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221111743.png" alt=""></p>
<ul>
<li><p>Oracle <code>JRockit</code> （原来的 Bea JRockit）电脑软件 (基本JRockit JVM是世界上最快的<a href="https://baike.baidu.com/item/JVM/2902369" target="_blank" rel="noopener">JVM</a>)</p>
</li>
<li><p>IBM<code>J9</code>  (<a href="https://www.zhihu.com/question/39780313?sort=created" target="_blank" rel="noopener">https://www.zhihu.com/question/39780313?sort=created</a>)</p>
</li>
</ul>
<h3 id="12-堆"><a href="#12-堆" class="headerlink" title="12. 堆"></a>12. 堆</h3><p><strong>Heap：一个JVM只有一个堆内存，堆内存的大小是可以调节的</strong></p>
<ul>
<li>类加载器读取了类文件后，一般会把什么东西放到堆中？<ul>
<li>类，常量，变量~</li>
<li>保存所有引用类型的真实对象</li>
</ul>
</li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221112550.png" alt=""></p>
<p>堆内存中还要细分为三个区域：</p>
<ul>
<li><p>新生区（伊甸园区） Young/new</p>
</li>
<li><p>养老区   old</p>
</li>
<li><p>永久区</p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221113037.png" alt=""></p>
</li>
</ul>
<h4 id="12-1-新生区，老年区，永久区"><a href="#12-1-新生区，老年区，永久区" class="headerlink" title="12.1 新生区，老年区，永久区"></a>12.1 新生区，老年区，永久区</h4><p><strong>GC垃圾回收，主要实在伊甸园区和养老区</strong></p>
<p><strong>假设内存满了：OOM</strong> (<strong>堆内存不够：OutOfMemoryError</strong>)</p>
<p><strong>在JDK8以后,永久存储区改了个名字（元空间）</strong></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200221113722.png" alt=""></p>
<h4 id="12-2-新生区"><a href="#12-2-新生区" class="headerlink" title="12.2 新生区"></a>12.2 新生区</h4><ul>
<li>类：诞生和成长的地方，甚至到死亡；</li>
<li>伊甸园，所有对象都是在伊甸园区new出来的</li>
<li>幸存者区（0，1）</li>
<li>真理：经过研究，99%的对象都是临时对象</li>
</ul>
<h4 id="12-3-永久区"><a href="#12-3-永久区" class="headerlink" title="12.3 永久区"></a>12.3 永久区</h4><p>这个区域常驻内存的，用来存放JDK自身携带的Class对象，Interface元数据，存储的使Java运行时的一些环境或类信息</p>
<p>这个区域不存在垃圾回收，关闭VM虚拟就会释放这个区域的内存</p>
<p>一个启动类，加载了大量的第三方jar包。Tomcat部署了太多的应用，大量动态生成的反射类。不断的被加载，直到内存满，就会出现OOM</p>
<ul>
<li>jdk 1.6:永久代，<strong>常量池在方法区</strong></li>
<li>jdk 1.7:永久代，但是慢慢退化了，去永久代，<strong>常量池在堆中</strong></li>
<li>jdk 1.8:无永久代，<strong>常量池在元空间</strong></li>
</ul>
<p><strong>元空间：逻辑上存在，物理上不存在</strong></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223095714.png" alt=""></p>
<h4 id="12-4-OOM-Error"><a href="#12-4-OOM-Error" class="headerlink" title="12.4 OOM Error"></a>12.4 OOM Error</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//OOM Error示例</span></span><br><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OOM</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String str = <span class="string">"Zhuuuu"</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">            str = str + <span class="keyword">new</span> Random().nextInt(<span class="number">899888888</span>) + <span class="keyword">new</span> Random().nextInt(<span class="number">999999999</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>输出：OutOfMemoryError</strong></p>
<p><strong>解决方法</strong>：</p>
<ol>
<li>尝试扩大堆内存（如下所示看结果）</li>
<li>分析内存，看一下哪个地方出现了问题</li>
</ol>
<h4 id="12-5-IDEA调节虚拟机堆内存大小"><a href="#12-5-IDEA调节虚拟机堆内存大小" class="headerlink" title="12.5 IDEA调节虚拟机堆内存大小"></a>12.5 IDEA调节虚拟机堆内存大小</h4><p>配置如下代码：在Configuration中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-Xms8m -Xmx8m -XX:+PrintGCDetails</span><br></pre></td></tr></table></figure>

<p>具体步骤如下图所示：</p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223102028.png" alt=""></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223100534.png" alt=""></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223111226.png" alt=""></p>
<p><strong>元空间：逻辑上存在，物理上不存在</strong></p>
<h4 id="12-6-扩展学习（Random-nextInt-）"><a href="#12-6-扩展学习（Random-nextInt-）" class="headerlink" title="12.6 扩展学习（Random.nextInt()）"></a>12.6 扩展学习（Random.nextInt()）</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">random.nextInt()的用法</span></span><br><span class="line"><span class="comment">1、不带参数的nextInt()会生成所有有效的整数（包含正数，负数，0）</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">2、带参的nextInt(int x)则会生成一个范围在0~x（不包含X）内的任意正整数</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">例如：int x=new Random.nextInt(100); </span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">则x为一个0~99的任意整数</span></span><br><span class="line"><span class="comment">　　　　</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>



<h3 id="13-堆内存调优"><a href="#13-堆内存调优" class="headerlink" title="13. 堆内存调优"></a>13. 堆内存调优</h3><h4 id="13-1-Jprofiler"><a href="#13-1-Jprofiler" class="headerlink" title="13.1 Jprofiler"></a>13.1 Jprofiler</h4><ul>
<li>分析Dump内存文件，快速定位内存泄露</li>
<li>获得堆中的数据</li>
<li>获得大的对象~</li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223105004.png" alt=""></p>
<h3 id="14-GC垃圾回收器"><a href="#14-GC垃圾回收器" class="headerlink" title="14. GC垃圾回收器"></a>14. GC垃圾回收器</h3><ul>
<li>JVM在进行GC中，并不是对三个区域进行统一回收。大部分时候，回收的都是新生代</li>
<li>GC的两种类：<ul>
<li>轻GC(普通的GC)</li>
<li>重GC(深度的GC)</li>
</ul>
</li>
</ul>
<h4 id="14-1-GC的作用区域"><a href="#14-1-GC的作用区域" class="headerlink" title="14.1 GC的作用区域"></a>14.1 GC的作用区域</h4><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223111437.png" alt=""></p>
<h4 id="14-2-GC面试题"><a href="#14-2-GC面试题" class="headerlink" title="14.2 GC面试题"></a>14.2 GC面试题</h4><ol>
<li><p>JVM内存模型和分区 详细到每个区放什么？</p>
</li>
<li><p>堆里面的分区有哪些？(Eden, from, to，老年区)</p>
</li>
<li><p>GC的算法有哪些？</p>
<ul>
<li>标记清除法</li>
<li>标记压缩法</li>
<li>复制算法</li>
<li>引用计数器</li>
</ul>
</li>
<li><p>轻GC和重GC分别用在什么时候？</p>
</li>
</ol>
<h4 id="14-3-GC-引用计数法"><a href="#14-3-GC-引用计数法" class="headerlink" title="14.3 GC 引用计数法"></a>14.3 GC 引用计数法</h4><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223112254.png" alt=""></p>
<h4 id="14-4-GC-复制算法"><a href="#14-4-GC-复制算法" class="headerlink" title="14.4 GC 复制算法"></a>14.4 GC 复制算法</h4><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223113151.png" alt=""></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223113452.png" alt=""></p>
<ul>
<li>步骤：</li>
</ul>
<ol>
<li>from区把垃圾丢给to</li>
<li>Eden区把垃圾丢给to</li>
</ol>
<ul>
<li>优点：</li>
</ul>
<ol>
<li>没有内存的碎片</li>
</ol>
<ul>
<li>缺点：</li>
</ul>
<ol>
<li>浪费了内存空间，多了一半空间永远是空的</li>
</ol>
<ul>
<li>复制算法最佳场景使用：对象存活度较低的时候（新生区中使用最好）</li>
</ul>
<h4 id="14-5-GC-标记清除法"><a href="#14-5-GC-标记清除法" class="headerlink" title="14.5 GC 标记清除法"></a>14.5 GC 标记清除法</h4><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223120429.png" alt=""></p>
<ul>
<li>优点：不需要额外的空间</li>
<li>缺点：两次扫描，严重浪费时间，会产生内存碎片</li>
</ul>
<h4 id="14-6-GC-标记压缩法"><a href="#14-6-GC-标记压缩法" class="headerlink" title="14.6 GC 标记压缩法"></a>14.6 GC 标记压缩法</h4><p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200223120715.png" alt=""></p>
<h4 id="14-7-GC算法总结"><a href="#14-7-GC算法总结" class="headerlink" title="14.7 GC算法总结"></a>14.7 GC算法总结</h4><ul>
<li>内存效率 ： 复制算法&gt;标记清除算法&gt;标记压缩算法（时间复杂度）</li>
<li>内存整齐度：复制算法=标记压缩算法&gt;标记清除算法</li>
<li>内存利用率：标记压缩算法=标记清除算法&gt;复制算法</li>
</ul>
<p><strong>没有最好的算法，只有一个最合适的算法</strong></p>
<p>GC:分代收集算法</p>
<p>年轻代：</p>
<ul>
<li>存活率低</li>
<li>复制算法</li>
</ul>
<p>老年代：</p>
<ul>
<li>区域大：存活率</li>
<li>标记清除（内存碎片不是太多） + 标记压缩混合 实现</li>
</ul>
<h3 id="15-JMM"><a href="#15-JMM" class="headerlink" title="15. JMM"></a>15. JMM</h3><p><strong>1. 什么是JMM?</strong></p>
<ul>
<li>Java 内存模型Java 内存模型（JMM）是一种抽象的概念，并不真实存在，它描述了一组规则或规范，</li>
<li>通过这组规范定义了程序中各个变量（包括实例字段、静态字段和构成数组对象的元素）的访问方式。</li>
<li>试图屏蔽各种硬件和操作系统的内存访问差异，以实现让 Java 程序在各种平台下都能达到一致的内存访问效果。</li>
<li>注意JMM与JVM内存区域划分的区别：JMM描述的是一组规则，围绕<strong>原子性</strong>、<strong>有序性</strong>和<strong>可见性</strong>展开</li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200225104802.png" alt=""></p>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200225104730.png" alt=""></p>
<p><strong>2. JMM的作用?</strong></p>
<p><strong>作用：缓存一致性协议，用于定义数据读取的规则（遵守规则）</strong></p>
<ul>
<li><p>JMM定义了线程工作内存和主内存之间的抽象关系：线程之间的共享变量存储在主内存（Main Memory）中，每个线程都有一个私有的本地内存。</p>
</li>
<li><p>所有的变量都存储在主内存当中，每个线程还有自己的工作内存，工作内存存储在高速缓存或者寄存器中，保存了该线程使用的变量的主内存副本拷贝。</p>
</li>
<li><p>线程只能直接操作工作内存中的变量，不同线程之间的变量值传递需要通过主内存来完成。</p>
</li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200225102824.png" alt=""></p>
<p><strong>解决共享对象可见性这个问题： volilate</strong></p>
<p><strong>3. JMM的规则</strong></p>
<p> 　内存交互操作有8种，虚拟机实现必须保证每一个操作都是原子的，不可再分的（对于double和long类型的变量来说，load、store、read和write操作在某些平台上允许例外）</p>
<ul>
<li><ul>
<li>lock   （锁定）：作用于主内存的变量，把一个变量标识为线程独占状态</li>
<li>unlock （解锁）：作用于主内存的变量，它把一个处于锁定状态的变量释放出来，释放后的变量才可以被其他线程锁定</li>
<li>read  （读取）：作用于主内存变量，它把一个变量的值从主内存传输到线程的工作内存中，以便随后的load动作使用</li>
<li>load   （载入）：作用于工作内存的变量，它把read操作从主存中变量放入工作内存中</li>
<li>use   （使用）：作用于工作内存中的变量，它把工作内存中的变量传输给执行引擎，每当虚拟机遇到一个需要使用到变量的值，就会使用到这个指令</li>
<li>assign （赋值）：作用于工作内存中的变量，它把一个从执行引擎中接受到的值放入工作内存的变量副本中</li>
<li>store  （存储）：作用于主内存中的变量，它把一个从工作内存中一个变量的值传送到主内存中，以便后续的write使用</li>
<li>write 　（写入）：作用于主内存中的变量，它把store操作从工作内存中得到的变量的值放入主内存的变量中</li>
</ul>
</li>
</ul>
<p><img src="https://zhuuu-bucket.oss-cn-beijing.aliyuncs.com/img/20200225104853.png" alt=""></p>
<p>　<strong>JMM对这八种指令的使用，制定了如下规则：</strong></p>
<ul>
<li><p>不允许read和load、store和write操作之一单独出现。即使用了read必须load，使用了store必须write</p>
</li>
<li><p>不允许线程丢弃他最近的assign操作，即工作变量的数据改变了之后，必须告知主存</p>
</li>
<li><p>不允许一个线程将没有assign的数据从工作内存同步回主内存</p>
</li>
<li><p>一个新的变量必须在主内存中诞生，不允许工作内存直接使用一个未被初始化的变量。就是怼变量实施use、store操作之前，必须经过assign和load操作</p>
</li>
<li><p>一个变量同一时间只有一个线程能对其进行lock。多次lock后，必须执行相同次数的unlock才能解锁</p>
</li>
<li><p>如果对一个变量进行lock操作，会清空所有工作内存中此变量的值，在执行引擎使用这个变量前，必须重新load或assign操作初始化变量的值</p>
</li>
<li><p>如果一个变量没有被lock，就不能对其进行unlock操作。也不能unlock一个被其他线程锁住的变量</p>
</li>
<li><p>对一个变量进行unlock操作之前，必须把此变量同步回主内存</p>
</li>
</ul>
<p>  JMM对这八种操作规则和对<a href="https://www.cnblogs.com/null-qige/p/8569131.html" target="_blank" rel="noopener">volatile的一些特殊规则</a>就能确定哪里操作是线程安全，哪些操作是线程不安全的了。但是这些规则实在复杂，很难在实践中直接分析。所以一般我们也不会通过上述规则进行分析。更多的时候，使用java的happen-before规则来进行分析。</p>
<p><strong>4. JMM和JVM的内存区域划分区别</strong></p>
<ul>
<li>JMM描述的是一组规则，围绕原子性，有序性和可见性展开</li>
<li>相似点：存在共享区域和私有区域</li>
</ul>
<h3 id="16-内存模型的三大特性"><a href="#16-内存模型的三大特性" class="headerlink" title="16. 内存模型的三大特性"></a>16. 内存模型的三大特性</h3><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p><strong>学习方案</strong></p>
<ol>
<li><p>百度</p>
</li>
<li><p>思维导图</p>
</li>
</ol>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://zhuuu.work/2020/02/24/JavaInterview/Java-%E5%9F%BA%E7%A1%80-JVM&JMM/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JMM/" rel="tag">JMM</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JVM/" rel="tag">JVM</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/02/26/JavaWeb/JavaWeb/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JavaWeb/JavaWeb
          
        </div>
      </a>
    
    
      <a href="/2020/02/22/JavaInterview/Java-%E5%9F%BA%E7%A1%80-%E5%A4%9A%E7%BA%BF%E7%A8%8B/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Java-基础-多线程</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
<div class="gitalk" id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.5.0/dist/gitalk.css">


<script src="https://cdn.jsdelivr.net/npm/gitalk@1.5.0/dist/gitalk.min.js"></script>


<script src="https://cdn.jsdelivr.net/npm/blueimp-md5@2.10.0/js/md5.min.js"></script>

<script type="text/javascript">
  var gitalk = new Gitalk({
    clientID: 'db188ed8c86dc4b0dbf3',
    clientSecret: 'a58f92160e5a9efd726b7d533000a0737f3e3f3e',
    repo: 'Blog-comments',
    owner: 'Zhuuuuuuuu',
    admin: ['Zhuuuuuuuu'],
    // id: location.pathname,      // Ensure uniqueness and length less than 50
    id: md5(location.pathname),
    distractionFreeMode: false,  // Facebook-like distraction free mode
    pagerDirection: 'last'
  })

  gitalk.render('gitalk-container')
</script>

  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2019-2021
        Zhuuu
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="朱酱酱的学习博客"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/JVM/">JVM</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/JDK%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90/">JDK源码</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/">多线程</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Mysql/">Mysql</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Redis/">Redis</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E8%AE%BE%E8%AE%A1%E8%80%85%E6%A8%A1%E5%BC%8F/">设计模式</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/MyBatis/">MyBatis</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/SpringMVC/">SpringMVC</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Spring/">Spring</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/SpringBoot/">SpringBoot</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">Linux</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Leetcode/">Leetcode</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E5%89%8D%E7%AB%AF/">前端</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/photoshop/">photoshop</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://smartzhuuu.lofter.com/" target="_blank" rel="noopener">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2020/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['昨夜西风凋碧树。独上高楼，望尽天涯路','衣带渐宽终不悔，为伊消得人憔悴。','众里寻他千百度。蓦然回首，那人却在，灯火阑珊处。'],
    startDelay: 0,
    typeSpeed: 200,
    loop: true,
    backSpeed: 100,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>